package cn.lele.reggie.controller;

import cn.lele.reggie.common.BaseContext;
import cn.lele.reggie.common.R;
import cn.lele.reggie.dto.UserDto;
import cn.lele.reggie.entity.User;
import cn.lele.reggie.service.UserService;
import cn.lele.reggie.utils.ValidateCodeUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private HttpServletRequest request;

    /**
     * 登录
     * @param user
     * @return
     */
    @PostMapping("/login")
    public R<String> login(@RequestBody UserDto user){
        return userService.login(user,request);
    }

    /**
     * 退出登录
     * @return
     */
    @PostMapping("/loginout")
   public R<String> loginout(){
        request.getSession().removeAttribute("userId");
        log.info("移除userId");
        return R.success("退出登录成功");
    }

    /**
     * 模拟发送验证码，顺便注册用户
     * @param user
     * @return
     */
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user){

        return userService.sendMsg(user);
    }

    /**
     * 根据用户id显示用户信息
     * @param
     * @return
     */
    @GetMapping("{id}")
    public R<User> selectById(@PathVariable Long id){
        User user = userService.getById(BaseContext.getCurrentId());
        return R.success(user);
    }

    /**
     * 修改用户信息+后台修改用户状态
     * @param user
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody User user){
        log.info("获取到用户信息,准备修改---》"+user);
        userService.updateById(user);
        return R.success("修改成功");
    }

    /**
     * 根据电话查id
     * @param phone
     * @return
     */
    @GetMapping("/selectId")
    public R<User> seleceId(String phone){
        LambdaUpdateWrapper<User> userLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        userLambdaUpdateWrapper.eq(phone!=null,User::getPhone,phone);
        User one = userService.getOne(userLambdaUpdateWrapper);
        return R.success(one);
    }

    /**
     * 后台用户管理
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize,String name){
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.like(name!=null,User::getName,name);
        Page<User> userPage = new Page<>(page, pageSize);
        userService.page(userPage,userLambdaQueryWrapper);
        return R.success(userPage);
    }


}
